package sg.edu.nus.iss.club.gui;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import sg.edu.nus.iss.club.*;
import sg.edu.nus.iss.club.util.*;

public class ClubApplication {

	private Club club ;
	private ClubWindow clubWindow ;
	private ClubFileStore clubFileStore;
	
	public ClubApplication() {
		//club = new Club();
		clubWindow = new ClubWindow(this);
		clubWindow.pack ();
		
	}
	

	public void start() {
		//clubWindow = new ClubWindow(this);
		
		club = clubFileStore.retrieve();
		clubWindow.setVisible(true);
	}
	
	public void shutdown() {
		clubFileStore.store(club);
		clubWindow.dispose();
		System.exit(0);
		
	}
	
	public ClubWindow getClubWindow() {
		
		return clubWindow;
	}
	public ArrayList <Member> getMembers() {
		return club.getMembers();
	}
	
	public void addMember(String surname, String firstname, String secondName){
		
		club.addMember(surname, firstname, secondName);
		clubWindow.getMemberPanel().refresh();
		
	}
	
	public void removeMemeber(int memberNum){
		club.removeMember(memberNum);
		clubWindow.getMemberPanel().refresh();
		
	}
	
	public static void main(String[] args) {
		SimpleDateFormat sdf = new SimpleDateFormat (Booking.DATE_FORMAT);
		String clubFileName = "club.dat"; //default
		// TODO Auto-generated method stub
		System.out.println ("Hello ClubApplication World!");
		if (args.length == 1) {
			clubFileName = args[0];
		}
		
		
		ClubApplication clubApplication = new ClubApplication();
		clubApplication.clubFileStore = new ClubFileStore(clubFileName);
		clubApplication.start();
		
		/*
		Person p1 = new Person ("sur1", "first1", "second1"); 
		//Person p1clone = new Person ("sur1", "first1", "second1");
		//System.out.println(p1.compareTo(p1clone));//test compareTo
		Person p2 = new Person ("sur2","first2");
		Person p3 = new Member ("sur3", "first3", "second3", 1);
		
		//p1.show();
		//p2.show();
		//p3.show();
		
	
		
		Club club = new Club();
		Member cm1 = club.addMember("cm1Sur", "cm1First", "cm1Second");
				
		club.addMember("cm2Sur", "cm2First", "cm2Second");
		club.addMember("cm3Sur", "cm3First", "cm3Second");
		
		
		Facility f1 = new Facility("conference room", "room 1-1");
		club.addFacility(new Facility("conference room", "room 1-1"));
		club.addFacility( new Facility("function room"));
		club.addFacility( new Facility("meeting room 1", "room 2-1"));

		//club.removeFacility("conference room");
		//club.removeFacility("conference");
		club.show();
		try {
			Date startDate1 = (Date)(sdf.parse("0213-12:02"));
			Date endDate1 = (Date)(sdf.parse("0213-13:02"));
			Booking booking1 = new Booking(cm1,f1,startDate1,endDate1);
			
			System.out.println(booking1);
		}catch(ParseException pe) {
			pe.printStackTrace();
		}catch(BadBookingException bbe){
			bbe.printStackTrace();
		}
		
		try {
		
		
		Date startDate2 = (Date)(sdf.parse("0214-12:02"));
		Date endDate2 = (Date)(sdf.parse("0214-13:02"));
		
		Date startDate3 = (Date)(sdf.parse("0215-12:02"));
		Date endDate3 = (Date)(sdf.parse("0215-13:02"));
		
		club.addBooking(1, "conference room", startDate2, endDate2);

		club.addBooking(2, "conference room", startDate3, endDate3);
		
		
		club.showBookings("conference room", startDate2, endDate3);
		}catch(ParseException pe) {
			pe.printStackTrace();
		}catch(BadBookingException bbe){
			bbe.printStackTrace();
		}
		*/
	}
	
	

}
